package com.bb.flow.parser.impl.parsejson.parsenode.nodeimp;

import com.alibaba.fastjson2.JSONObject;
import com.bb.flow.api.exception.ParseException;
import com.bb.flow.config.AbstractConfigNode;
import com.bb.flow.config.node.SubFlowConfigNode;

import static com.bb.flow.util.Tools.isEmptyOrNull;

/**
 * @author bigbeard
 * 解析子流程类型的节点
 */
public class ParseSubFlowNode extends AbstractNodeImp {

    @Override
    public AbstractConfigNode parse(JSONObject jsonObject, String flowTemplateName, String parentNodeKey) throws ParseException {
        SubFlowConfigNode configNode = new SubFlowConfigNode();
        boolean b = parseNodeBaseInfo.parseNodeBaseInfo(configNode, jsonObject, flowTemplateName);
        if (!b) {
            return null;
        }
        String nodeKey = getNodeKey(parentNodeKey, configNode);
        try {
            Boolean needCheckCondition = jsonObject.getBoolean("NeedCheckCondition");
            configNode.setNeedCalcStartCondition(needCheckCondition);
        } catch (Exception ex) {
            logger.warn("流程模版[{}]中的节点[{}]的是否计算流程启动条件[NeedCheckCondition]属性配置错误",
                    flowTemplateName, nodeKey);
            configNode.setNeedCalcStartCondition(false);
        }
        String subFlowTemplateName = jsonObject.getString("FlowTemplateName");
        if (isEmptyOrNull(subFlowTemplateName)) {
            String s = String.format("流程模版[%s]中的节点[%s]中的子流程模版名称[FlowTemplateName]属性为空",
                    flowTemplateName, nodeKey);
            throw new ParseException(s);
        }
        configNode.setSubFlowTemplateName(subFlowTemplateName);
        return configNode;
    }

}
